/**
 IDEA Configurator
 Copyright (C) 2006 Justin Tomich<tomichj at gmail dot com>

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License as
 published by the Free Software Foundation; either version 2 of the
 License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful, but
 WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

package configurator.task;

import configurator.ModuleBuilder;

import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.Project;

import java.io.File;


/**
 * @author Justin Tomich
 */
public class ModuleLibrarySubtask implements DependencySubtask
{
    private boolean exported = false;
    private String javadocUrl;
    private Path classes;
    private Path sources;
    private Path javadocs;


    public ModuleLibrarySubtask(Project project)
    {
        classes = new Path(project);
        sources = new Path(project);
        javadocs = new Path(project);
    }


    public void setExported(boolean exported)
    {
        this.exported = exported;
    }


    public void setClasses(File classes)
    {
        this.classes.setLocation(classes);
    }


    public void setSources(File sources)
    {
        this.sources.setLocation(sources);
    }


    public void setJavadoc(File javadoc)
    {
        this.javadocs.setLocation(javadoc);
    }


    public void setJavadocUrl(String javadocUrl)
    {
        this.javadocUrl = javadocUrl;
    }


    public Path createClasses()
    {
        return classes.createPath();
    }


    public Path createSources()
    {
        return sources.createPath();
    }


    public Path createJavadoc()
    {
        return javadocs.createPath();
    }


    public void addTo(ModuleBuilder builder)
    {
        String[] jdUrl = javadocUrl != null ?
                         new String[] {javadocUrl} : new String[] {};
        builder.newModuleLibrary(array(classes), array(sources),
                array(javadocs), jdUrl, exported);
    }


    private String[] array(Path path)
    {
        if (path == null) return new String[] {};
        if (path.size() == 0) return new String[] {};
        return path.list();
    }


    public String toString()
    {
        return "Library{" +
               "classes=" + classes +
               ", sources=" + sources +
               ", javadoc=" + javadocs +
               ", javadocUrl='" + javadocUrl + "'" +
               "}";
    }
}
